#include "stdafx.h"
#include "InputUtil.h"

using namespace input;

bool pointInRect(const Point & p, const Rect & rect)
{
	return
		p.X >= rect.X &&
		p.X <= rect.X + rect.Width &&
		p.Y >= rect.Y &&
		p.Y <= rect.Y + rect.Height;
}

input::Event input::convertWindowsMsgToEvent( UINT msg, WPARAM wParam, LPARAM lParam )
{
	Event event;
	event.Type = Event::OTHER;

	switch (msg)
	{
	case WM_MOUSEMOVE:
		event.Type = Event::MOUSE;
		event.MouseEv.Type = MouseEvent::MOVE;
		event.MouseEv.Pos = Point(LOWORD(lParam), HIWORD(lParam));
		break;

	case WM_LBUTTONDOWN:
		event.Type = Event::MOUSE;
		event.MouseEv.Type = MouseEvent::L_DOWN;
		event.MouseEv.Pos = Point(LOWORD(lParam), HIWORD(lParam));
		break;

	case WM_LBUTTONUP:
		event.Type = Event::MOUSE;
		event.MouseEv.Type = MouseEvent::L_UP;
		event.MouseEv.Pos = Point(LOWORD(lParam), HIWORD(lParam));
		break;

	case WM_MBUTTONDOWN:
		event.Type = Event::MOUSE;
		event.MouseEv.Type = MouseEvent::M_DOWN;
		event.MouseEv.Pos = Point(LOWORD(lParam), HIWORD(lParam));
		break;

	case WM_MBUTTONUP:
		event.Type = Event::MOUSE;
		event.MouseEv.Type = MouseEvent::M_UP;
		event.MouseEv.Pos = Point(LOWORD(lParam), HIWORD(lParam));
		break;

	case WM_RBUTTONDOWN:
		event.Type = Event::MOUSE;
		event.MouseEv.Type = MouseEvent::R_DOWN;
		event.MouseEv.Pos = Point(LOWORD(lParam), HIWORD(lParam));
		break;

	case WM_RBUTTONUP:
		event.Type = Event::MOUSE;
		event.MouseEv.Type = MouseEvent::R_UP;
		event.MouseEv.Pos = Point(LOWORD(lParam), HIWORD(lParam));
		break;

	case WM_MOUSEWHEEL:
		event.Type = Event::MOUSE;
		event.MouseEv.Type = MouseEvent::WHEEL;
		event.MouseEv.Pos = Point(LOWORD(lParam), HIWORD(lParam));
		event.MouseEv.WheelAmount = GET_WHEEL_DELTA_WPARAM(wParam);
		break;

	case WM_KEYDOWN:
		event.Type = Event::KEY;
		event.KeyEv = convert_WM_KEYDOWN_ToKeyEvent(wParam, lParam);
		break;

	case WM_CHAR:
		event.Type = Event::KEY;
		event.KeyEv = convert_WM_CHAR_ToKeyEvent(wParam, lParam);
		break;
	}
	return event;
}

input::KeyEvent input::convert_WM_CHAR_ToKeyEvent( WPARAM wParam, LPARAM lParam )
{
	KeyEvent event;
	switch (wParam)
	{
	case 0x08:
		event.PressedKey = KeyEvent::KEY_BACKSPACE;
		break;
	case 0x0A:
		event.PressedKey = KeyEvent::KEY_ENTER;
		break;
	case 0x1B:
		event.PressedKey = KeyEvent::KEY_ESCAPE;
		break;
	case 0x09:
		event.PressedKey = KeyEvent::KEY_TAB;
		break;
	case 0x0D:
		event.PressedKey = KeyEvent::KEY_CR;
		break;
	default:
		event.PressedKey = KeyEvent::KEY_CHAR;
		event.Char = (TCHAR)wParam;
		break;
	}
	return event;
}

input::KeyEvent input::convert_WM_KEYDOWN_ToKeyEvent( WPARAM wParam, LPARAM lParam )
{
	KeyEvent event;
	switch (wParam)
	{
	case VK_LEFT:
		event.PressedKey = KeyEvent::KEY_LEFT;
		break;
	case VK_RIGHT:
		event.PressedKey = KeyEvent::KEY_RIGHT;
		break;
	case VK_UP:
		event.PressedKey = KeyEvent::KEY_UP;
		break;
	case VK_DOWN:
		event.PressedKey = KeyEvent::KEY_DOWN;
		break;
	case VK_SPACE:
		event.PressedKey = KeyEvent::KEY_SPACE;
		break;
	case VK_SHIFT:
		event.PressedKey = KeyEvent::KEY_SHIFT;
		break;
	case VK_RETURN:
		event.PressedKey = KeyEvent::KEY_ENTER;
		break;
	default:
		event.PressedKey = KeyEvent::KEY_UNKNOWN;
		break;
	}
	return event;
}
